package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.GoodsMapper;
import com.hqyj.dao.OrderMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.Order;
import com.hqyj.pojo.User;
import com.hqyj.service.GoodsService;
import com.hqyj.utile.GetTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;
    public HashMap<String, Object> showGoodsList(Goods goods ,int i) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        switch (i){
            case 0 :
                List<Goods> list =  goodsMapper.showGoodsList();
                setSaleNumber(list);
                System.out.println("当前的商品的页数为："+goods.getPage()+"每行多少个？"+goods.getRow());
                PageHelper.startPage(goods.getPage(),goods.getRow());
                PageInfo<Goods> pageInfo = new PageInfo<Goods>(list);
                System.out.println("你到底有好多页哦？"+pageInfo.getPages());
                map.put("list",pageInfo.getList());
                //6.总页数
                Long total = pageInfo.getTotal();
                int totalPage = pageInfo.getPages();
                map.put("total",total);
                map.put("totalPage",totalPage);
                break;
            case 1 :
                List<Goods> list1 =  goodsMapper.showGoodsList1();
                setSaleNumber(list1);
                setListAway(goods,list1,map);
                break;
            case 2 :
                List<Goods> list2 =  goodsMapper.showGoodsList2();
                setSaleNumber(list2);
                setListAway(goods,list2,map);
                break;
            case 3 :
                List<Goods> list3 =  goodsMapper.showGoodsList3();
                setSaleNumber(list3);
                setListAway(goods,list3,map);
                break;
            case 4 :
                List<Goods> list4 =  goodsMapper.showGoodsList4();
                setSaleNumber(list4);
                setListAway(goods,list4,map);
                break;
            case 5 :
                List<Goods> list5 =  goodsMapper.showGoodsList5();
                setSaleNumber(list5);
                setListAway(goods,list5,map);
                break;
        }

        return map;
    }

    private void setListAway(Goods goods, List<Goods> list1,HashMap<String, Object> map) {
        System.out.println("当前的商品的页数为："+goods.getPage()+"每行多少个？"+goods.getRow());
        PageHelper.startPage(goods.getPage(),goods.getRow());
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(list1);
        System.out.println("你到底有好多页哦？"+pageInfo.getPages());
        map.put("list",pageInfo.getList());
        //6.总页数
        Long total = pageInfo.getTotal();
        int totalPage = pageInfo.getPages();
        map.put("total",total);
        map.put("totalPage",totalPage);

    }

    private void setSaleNumber(List<Goods> list) {
        String saleNumber = null;
        for (Goods goodss : list) {
            saleNumber = goodsMapper.selectGoodSalesByGoodsId(goodss.getgId());
            System.out.println("商品销量为：" + saleNumber);
            if (saleNumber!=null){
                goodss.setSaleNumber(Integer.parseInt(saleNumber));
            }else {
                goodss.setSaleNumber(0);
            }

        }
    }
    public HashMap<String, Object> selectGoodsPhotos(Goods goods, HttpServletRequest request, MultipartFile file) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println(goods.getgName()+"===================");
        //设置商品的编号
        Random r = new Random();
        String ran2 = "#" + r.nextInt(500);
        goods.setgId(ran2);

        //设置商家的名字
        User user = userMapper.selectByPrimaryKey(goods.getgUserId());
        String gOwnerName = user.getUserName();
        System.out.println("===================="+gOwnerName);
        goods.setgOwnerName(gOwnerName);

        //设置上传的时间
        Date date = new Date();
        goods.setgSaleTime(date);

        if (file.isEmpty()) {
            request.setAttribute("message","该文件为空");
            map.put("message","上传失败！");
            return map;
        }
        System.out.println("===================="+file.getOriginalFilename());
        String destPath = "C:/Users/王炼/Desktop/4月29日/shoppingSystem/src/main/webapp/images/"+file.getOriginalFilename();
        String relativePath = "/"+file.getOriginalFilename();
        goods.setgPhotoUrl(relativePath);

        //设置字段
        goods.setField(1);

        goodsMapper.insertSelective(goods);

        File destFile = new File(destPath);
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        map.put("info","上传成功！");
        return map;
    }

    public HashMap<String, Object> insertIntoNewBuy(Goods goods, String userName) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Order order = new Order();
        order.setField1(1);
        order.setgId(goods.getgId());
        order.setgName(goods.getgName());
        order.setgNumber(goods.getgNumber());
        order.setUserId(userMapper.selectUserIdByName(userName));
        Date nowTime = new Date();
        order.setgSaleTime(nowTime);
        int i = orderMapper.insertSelective(order);
        if (i != 0){
            map.put("info","购买成功");
            goodsMapper.subGoodsNumber(goods.getgId(),goods.getgNumber());
        }
        return map;
    }

    public HashMap<String, Object> selectHavedBuyGoods(Goods goods, String userName) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            int userId = userMapper.selectUserIdByName(userName);
            List<Goods> list =  orderMapper.showGoodsListByUserName(userId);
        for (Goods one:list) {
            one.setgPhotoUrl(goodsMapper.selectPhotoUrlByGoodsName(one.getgId()));
            one.setgPrice(goodsMapper.selectPriceByGoodsName(one.getgId()));
        }
            System.out.println("============"+list);
            System.out.println("当前的商品的页数为："+goods.getPage()+"每行多少个？"+goods.getRow());
            PageHelper.startPage(goods.getPage(),goods.getRow());
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(list);
            System.out.println("你到底有好多页哦？"+pageInfo.getPages());
            map.put("list",pageInfo.getList());
            //6.总页数
            Long total = pageInfo.getTotal();
            int totalPage = pageInfo.getPages();
            map.put("total",total);
            map.put("totalPage",totalPage);

            return map;
    }
}
